﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartAgricultureService.Core;
using SmartAgricultureService.Models.Tables;

namespace SmartAgricultureService.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class AnalysisController(DatabaseContext database, IHttpContextAccessor httpContextAccessor) : ControllerBase(httpContextAccessor)
    {
        [HttpGet("getDeviceData")]
        public async Task<IActionResult> GetDeviceData(string deviceId, DateTime dateTime)
        {
            try
            {
                var deviceEntry = await database.Devices.Include(o => o.Area).AsNoTracking().FirstOrDefaultAsync(x => x.Id == deviceId && x.Active && !x.Delete);
                if (deviceEntry == null)
                    return Reject("不存在该设备");
                else if (!await database.IsMatchArea(deviceEntry.Area.Code))
                    return Reject("设备不在指定的区域内，无法获取！");
                else
                {
                    var uploads = await database.Uploads.AsNoTracking().Where(x => EF.Functions.DateDiffDay(x.CreateDate, dateTime) == 0 && x.DeviceId == deviceId).ProjectToType<UploadInfo>(UploadInfo.GetAdapterConfig()).ToListAsync();
                    return Success(uploads);
                }
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }
        [HttpGet("getAreaData")]
        public async Task<IActionResult> GetAreaData(string areaCode, DateTime dateTime)
        {
            try
            {
                var deviceEntry = await database.Devices.Include(o => o.Area).AsNoTracking().FirstOrDefaultAsync(x => x.Area.Code == areaCode && x.Active && !x.Delete);
                if (deviceEntry == null)
                    return Reject("不存在该设备");
                else if (!await database.IsMatchArea(deviceEntry.Area.Code))
                    return Reject("设备不在指定的区域内，无法获取！");
                else
                {
                    var uploads = await database.Uploads.AsNoTracking().Where(x => EF.Functions.DateDiffDay(x.CreateDate, dateTime) == 0 && x.DeviceId == deviceEntry.Id).Select(x => new { x.Illumination }).ProjectToType<UploadInfo>(UploadInfo.GetAdapterConfig()).ToListAsync();
                    return Success(uploads);
                }
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }
        [HttpGet("getLogs")]
        public async Task<IActionResult> GetLogs(string deviceId, DateTime dateTime)
        {
            try
            {
                var deviceEntry = await database.Devices.Include(o => o.Area).AsNoTracking().FirstOrDefaultAsync(x => x.Id == deviceId && x.Active && !x.Delete);
                if (deviceEntry == null)
                    return Reject("不存在该设备");
                else if (!await database.IsMatchArea(deviceEntry.Area.Code))
                    return Reject("设备不在指定的区域内，无法获取！");
                else
                {
                    var logs = await database.Logs.AsNoTracking().Where(x => x.DeviceId == deviceId && EF.Functions.DateDiffDay(x.CreateDate, dateTime) == 0).ProjectToType<LogInfo>(LogInfo.GetAdapterConfig()).ToListAsync();
                    return Success(logs);
                }
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }
    }
}